﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication1.BLL;
using Millionaire;

namespace WindowsFormsApplication1
{
    public partial class FrmCheDoChoi : Form
    {
        private NguoiChoiBLL nguoiChoiBLL;
        private LinhVucBLL linhVucBLL;

        public FrmCheDoChoi()
        {
            InitializeComponent();
            nguoiChoiBLL = new NguoiChoiBLL();
            linhVucBLL = new LinhVucBLL();
            lbNguoiChoi.DisplayMember = "HoTen";
            lbNguoiChoi.ValueMember = "MaNguoiChoi";
            lbLinhVuc.DisplayMember = "TenLinhVuc";
            lbLinhVuc.ValueMember = "MaLinhVuc";
        }

        private void FrmCheDoChoi_Load(object sender, EventArgs e)
        {
            LoadDataToLbNguoiChoi();
            LoadDataToLbLinhVuc();
        }

        private void LoadDataToLbLinhVuc()
        {
            List<LinhVuc> list = linhVucBLL.GetAll();
            list.Insert(0, new LinhVuc() { MaLinhVuc = 0, TenLinhVuc = "Tất cả" });
            lbLinhVuc.DataSource = list;
        }

        private void LoadDataToLbNguoiChoi()
        {
            lbNguoiChoi.DataSource = nguoiChoiBLL.GetAll();
        }

        private void btnTaoMoi_Click(object sender, EventArgs e)
        {
            string hoten = txtNguoiChoi.Text;
            NguoiChoi nc = new NguoiChoi() { HoTen = hoten };
            if (nguoiChoiBLL.AddNew(nc))
            {
                LoadDataToLbNguoiChoi();
                lbNguoiChoi.SelectedItem = nc;
            }

        }

        private void txtBatDau_Click(object sender, EventArgs e)
        {
            List<int> listLV = new List<int>();
            for (int i = 0; i < lbLinhVuc.SelectedItems.Count; i++)
            {
                listLV.Add(((LinhVuc)lbLinhVuc.SelectedItems[i]).MaLinhVuc);
            }
            NguoiChoi nc = (NguoiChoi)lbNguoiChoi.SelectedItem;
            FrmGame frm = new FrmGame(listLV,nc);
            frm.ShowDialog();
        }
    }
}
